package com.yoga.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoga.model.Member;
import com.yoga.model.search.MemberSearchBean;
import com.yoga.service.MemberService;
import com.yoga.service.UploadService;
import com.yoga.utils.AliOSS;
import com.yoga.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author yan
 * @date 2025/9/8
 */
@RestController
@RequestMapping(value = "/api/v1/members",produces = MediaType.APPLICATION_JSON_VALUE)
public class MemberApi {
    private MemberService memberService;
    private AliOSS aliOSS;
    @Autowired
    public void setAliOSS(AliOSS aliOSS) {
        this.aliOSS = aliOSS;
    }

    @Autowired
    public void setMemberService(MemberService memberService) {
        this.memberService = memberService;
    }
    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(@RequestParam(defaultValue = "1") @Validated  Integer pageNo,
                                                 @RequestParam(defaultValue = "10") @Validated Integer pageSize,
                                                 MemberSearchBean msb){
        //分页对象
        Page<Member> page = new Page<>(pageNo,pageSize);
        page = this.memberService.findAll(page,msb);
        //System.out.println(page);
        return ResponseEntity.ok(JsonResult.success(page));
    }

    @GetMapping("/all")
    public ResponseEntity<JsonResult<?>> findAll() {
        List<Member> members = this.memberService.findAll();
        return ResponseEntity.ok(JsonResult.success(members));
    }


    @PutMapping("/deleted")
    public ResponseEntity<JsonResult<?>> deletedByIds(@RequestBody Map<String, Object> requestData){
        Integer[] ids = ((List<Integer>) requestData.get("ids")).toArray(new Integer[0]);
        Integer deleted = (Integer) requestData.get("deleted");
        int count = this.memberService.deletedByIds(List.of(ids), deleted);
        if(count == 0){
            return ResponseEntity.ok(JsonResult.fail("删除失败"));
        } else {
            return ResponseEntity.ok(JsonResult.success(count));
        }
    }

    @PostMapping
    public ResponseEntity<JsonResult<?>> add(@RequestBody @Validated Member member){
        //System.out.println(member);
        boolean flag = this.memberService.add(member);
        if (flag){
            return ResponseEntity.ok(JsonResult.success(member));
        }else {
            return ResponseEntity.ok(JsonResult.fail("添加失败"));
        }
    }

    @PutMapping
    public ResponseEntity<JsonResult<?>> update(@RequestBody @Validated Member member){
        boolean flag = this.memberService.update(member);
        if (flag){
            return ResponseEntity.ok(JsonResult.success(member));
        }else {
            return ResponseEntity.ok(JsonResult.fail("修改失败"));
        }
    }

    @PostMapping("/avatar")
    public ResponseEntity<JsonResult<?>> uploadAvatar(MultipartFile file) throws IOException {
        String path = this.aliOSS.upload(file,null);
        return ResponseEntity.ok(JsonResult.success(path));
    }
//    public ResponseEntity<JsonResult<?>> uploadAvatar(MultipartFile file){
//        String path = this.uploadService.uploadImage(file,"profile_picture");
//        return ResponseEntity.ok(JsonResult.success(path));
//    }
}
